package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.itheima.service.impl
 * @ClassName: OrderSettingServiceImpl
 * @Author: XLB
 * @Date: 2020/3/5 20:19
 * @Description:
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList!=null&&orderSettingList.size()>0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //检查数据库是否已近进行了预约设置
               long countByOrderDate= orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
               if (countByOrderDate>0) {
                   //已经进行了预约设置。执行更新操作
                   orderSettingDao.editNumberByOrderDate(orderSetting);

               }else {
                   //没有进行预约设置，执行插入操作
                   orderSettingDao.add(orderSetting);
               }
            }

        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String startData= date+"-1";
        String endData= date+"-31";
        Map<String,String> map=new HashMap<>();
        map.put("startData",startData);
        map.put("endData",endData);
        List<OrderSetting> orderSettingList= orderSettingDao.getOrderSettingByMonth(map);
        List<Map> mapList =new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map map1=new HashMap();
            map1.put("date",orderSetting.getOrderDate().getDate());//获得日期是几号
            map1.put("number",orderSetting.getNumber());
            map1.put("reservations",orderSetting.getReservations());
            mapList.add(map1);
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count>0){
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }

    }
}
